package org.grow.jssp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:宿世新
 * @Dte:2022/3/10
 */
@Controller
public class AuthController {
    @GetMapping("/index")
    public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("----------------");
        List<String> results = Arrays.asList("zhangsan", "lisi", "wangwu");
        String userName = "Lucy";
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/login.jsp");
        modelAndView.addObject("friends", results);
        modelAndView.addObject("username", userName);
        return modelAndView;
    }

    @PostMapping("/auth")
    public ModelAndView auth(HttpServletRequest request, HttpServletResponse response) {
        String password = request.getParameter("password");
        String username = request.getParameter("username");
        System.out.println("username：" + username + "尝试登陆");
        request.getSession().setAttribute("principle", username);
        ModelAndView modelAndView = new ModelAndView("redirect:repository");
        return modelAndView;
    }
}
